package mobi.greenplate.theweb.domain;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;
import mobi.greenplate.theweb.persistence.UnonwedLoader;

@Entity
public class Transport implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Basic
    private String transportName;
    @Basic
    private String transportDescription;
    @Enumerated(EnumType.STRING)
    private TransportType type;
    //ManyToOne - unowned
    @Basic
    private Long ownerId;
    @Transient
    private User owner;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + "[id=" + this.getId() + "]";
    }

    public String getTransportName() {
        return transportName;
    }

    public void setTransportName(String transportName) {
        this.transportName = transportName;
    }

    public String getTransportDescription() {
        return transportDescription;
    }

    public void setTransportDescription(String transportDescription) {
        this.transportDescription = transportDescription;
    }

    public TransportType getType() {
        return type;
    }

    public void setType(TransportType type) {
        this.type = type;
    }

    public User getOwner() {
        if (null == ownerId) {
            return null;
        }
        if (null != owner && owner.getId().equals(ownerId)) {
            return owner;
        }
        owner = UnonwedLoader.loadById(User.class, ownerId);
        return owner;
    }

    public void setOwner(User owner) {
        this.ownerId = owner.getId();
    }
}
